//
//  CityDynamicTotalViewController.m
//  cityStar
//
//  Created by wangjia on 14/12/13.
//  Copyright (c) 2014年 tiny. All rights reserved.
//

#import "CityDynamicTotalViewController.h"
#import "HTDefine.h"
#import "ColorUtil.h"
#import "CityDynamicViewController.h"
#import "CommonUtil.h"

@interface CityDynamicTotalViewController ()<UIScrollViewDelegate>{
    NSArray *aryName;
}

@end

@implementation CityDynamicTotalViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    aryName = [[NSArray alloc]init];
    self.navigationItem.title = @"城市动态";
    
    [self addBackButton];
    

    [self loadCategory];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)loadCategory{
    NSString *strUrl = [@"" stringByAppendingFormat:@"%@",KinterFace_NewsCategory];
    
    [HttpRequestManager getMethod:[strUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] parameters:nil completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
        if (success) {
            aryName = response[@"value"];
            [self setUIData];
        }
    }];
}

- (void)setUIData{
    self.segTitle.sectionTitles = aryName;
    
    self.segTitle.frame = CGRectMake(0, 0, KScreenWidth, 40);
    self.segTitle.backgroundColor = [UIColor whiteColor];
    self.segTitle.font = [UIFont boldSystemFontOfSize:15.0];
    self.segTitle.scrollEnabled = YES;
    self.segTitle.textColor = [ColorUtil colorFromHexRGB:@"999999"];
    self.segTitle.selectedTextColor = [ColorUtil colorFromHexRGB:@"ff5657"];
    self.segTitle.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationDown;
    self.segTitle.selectionStyle = HMSegmentedControlSelectionStyleFullWidthStripe;
    self.segTitle.selectionIndicatorHeight = 4;
    self.segTitle.segmentEdgeInset = UIEdgeInsetsMake(0, 10, 0, 0);
    self.segTitle.backgroundColor = rgba(247, 247, 247, 1);
    __weak typeof(self) weakSelf = self;
    [self.segTitle setIndexChangeBlock:^(NSInteger index) {
        [weakSelf changeSelect:index animation:YES];
    }];
    UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(0, 39, KScreenWidth, 1)];
    lbl.backgroundColor = [ColorUtil colorFromHexRGB:@"dbdbdb"];
    
    [self.view addSubview:self.segTitle];
    [self.segTitle addSubview:lbl];
    
    self.svContent.backgroundColor = [UIColor whiteColor];
    self.svContent.pagingEnabled = YES;
    self.svContent.showsHorizontalScrollIndicator = NO;
    self.svContent.showsVerticalScrollIndicator = NO;
    self.svContent.contentSize = CGSizeMake(KScreenWidth*aryName.count, self.svContent.frame.size.height);
    self.svContent.delegate = self;
    
    [self performSelector:@selector(selectAction) withObject:nil afterDelay:0.1];
}

- (void)selectAction{
    [self changeSelect:0 animation:NO];
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    KeyBoardDown();
    CGFloat pageWidth = scrollView.frame.size.width;
    NSInteger page = scrollView.contentOffset.x / pageWidth;
    [self.segTitle setSelectedSegmentIndex:page animated:YES];
    [self addViewController:page];
    
    for (UIViewController *vc in self.childViewControllers) {
        if ([vc isKindOfClass:[CityDynamicViewController class]]) {
            CityDynamicViewController *viewController = (CityDynamicViewController *)vc;
            viewController.tableView.scrollEnabled = YES;
        }
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    for (UIViewController *vc in self.childViewControllers) {
        if ([vc isKindOfClass:[CityDynamicViewController class]]) {
            CityDynamicViewController *viewController = (CityDynamicViewController *)vc;
            viewController.tableView.scrollEnabled = NO;
        }
    }
}

- (void)changeSelect:(NSInteger)intSelect animation:(BOOL)animation{
    KeyBoardDown();
    [self.segTitle setSelectedSegmentIndex:intSelect animated:animation];
    [self.svContent setContentOffset:CGPointMake(KScreenWidth*intSelect, 0) animated:animation];
    [self addViewController:intSelect];
}

- (void)addViewController:(NSInteger)index{
    NSMutableArray *maryIndex = [[NSMutableArray alloc]init];
    
    for (UIViewController *vc in self.childViewControllers) {
        if ([vc isKindOfClass:[CityDynamicViewController class]]) {
            [maryIndex addObject:[NSNumber numberWithInteger:vc.view.tag]];
        }
    }
    
    if (![maryIndex containsObject:[NSNumber numberWithInteger:index]]) {
        CityDynamicViewController *htSl = (CityDynamicViewController *)[CommonUtil getVCWithSbIden:@"Index" identify:@"SICityContent"];
        NSString *strName = aryName[index];
        htSl.intID = [self getIDFromName:strName];
        [self addChildViewController:htSl];
        htSl.view.frame = CGRectMake(KScreenWidth*index, 0, self.svContent.frame.size.width, self.svContent.frame.size.height);
        [self.svContent addSubview:htSl.view];
        htSl.view.tag = index;
    }
}

- (int)getIDFromName:(NSString *)name{
    NSArray *aryNameTotal = @[@"全球",@"财经",@"股票",@"城市新闻",@"城市民生",@"城市315",@"娱乐",@"体育",@"军事",@"科技",@"房产"];
    
    for (int i=0; i<aryNameTotal.count; i++) {
        if ([aryNameTotal[i] isEqualToString:name]) {
            return i;
        }
    }
    
    return 0;
}


#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}


@end
